package com.blue.base.common.bean.validators;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author liulei
 * @version 1.0
 */
public class PhoneNoValidator implements ConstraintValidator<PhoneNo, String> {

    private static final String PHONE_REX = "^[1](([3|5|8][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";

    @Override
    public void initialize(PhoneNo constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        if (value.matches(PHONE_REX)) {
            return true;
        }
        return false;
    }
}
